import { AfterViewInit, Component, ElementRef, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { takeUntil } from 'rxjs/operators';

import { BaseDirective } from '../../models/base-directive.model';
import { AuthenticationService } from '../../services/authentication.service';

@Component({
    selector: 'op-topbar',
    templateUrl: './topbar.component.html'
})
export class TopbarComponent extends BaseDirective implements OnInit, AfterViewInit {
    data: any[] = [];
    user: any;

    constructor(
        private authenticationService: AuthenticationService,
        private elementRef: ElementRef,
        private router: Router
    ) {
        super();
    }

    ngOnInit(): void {
        this.authenticationService.currentUser$.pipe(takeUntil(this.ngUnsubscribe)).subscribe(p => this.user = p);
        let count: number = 3;

        for (var index = 0; index < count; index++) {
            this.data.push(index);
        }
    }

    ngAfterViewInit(): void {
        // this.viewInitCompleted.emit(true);

        this.commonService.appendScriptChildToElement(this.elementRef, ['js/custom-topbar.js']);
    }

    lockScreen(): void {
        this.authenticationService.lockScreen();
    }

    logout(): void {
        this.authenticationService.removeUserInfo()
        this.router.navigate(['/authentication/login']);
    }
}